﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace RAC
{
    class RenameString 
    {
        private string rename;
        public enum RenameType
        {
            Normal,
            ToUpper,
            ToLower,
            FirstToUpper,
            FirstToLower,
            ToTitleCase,
            PreUP,
            MaxRename,
        };
        //-------------------------------------------------------------------
        // コンストラクタ
        //-------------------------------------------------------------------
        public RenameString(string str)
        {
            rename = str;
        }
        //-------------------------------------------------------------------
        // プラスリネーム
        //-------------------------------------------------------------------
        public RenameString PlusRename(string strPlus)
        {
            //特定の文字列を追加
            string strRename = "";
            int size = rename.Length;
            for (int i = 0; i < rename.Length; i++)
            {
                if (i != 0 && char.IsUpper(rename[i]))
                {
                    strRename += strPlus;
                }
                strRename += rename[i];
            }
            rename = strRename;
            return this;
        }
        //-------------------------------------------------------------------
        // タイプリネーム
        //-------------------------------------------------------------------
        public string TypeRename(RenameType type)
        {
            string strRename = ""; 
            switch (type)
            {
                case RenameType.Normal:
                    strRename = rename;
                    break;

                case RenameType.ToUpper:
                    strRename = rename.ToUpper();
                    break;

                case RenameType.ToLower:
                    strRename = rename.ToLower();
                    break;

                case RenameType.FirstToUpper:
                    if (rename != "")
                    {
                        strRename = char.ToUpper(rename[0]) + rename.Substring(1);
                    }
                    break;

                case RenameType.FirstToLower:
                    if (rename != "")
                    {
                        strRename = char.ToLower(rename[0]) + rename.Substring(1);
                    }
                    break;

                case RenameType.ToTitleCase:
                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    strRename = textInfo.ToTitleCase(rename);
                    break;

            }
            return strRename;
        }
        //-------------------------------------------------------------------
        // 置換リネーム
        //-------------------------------------------------------------------
        public string ReplaceRename(bool[] bFlag, RenameString strBName, RenameString strAName)
        {
            string strChange = strBName.TypeRename(RenameType.Normal);

            if (bFlag[(int)RenameType.Normal])
            {
                rename = rename.Replace(
                    strBName.TypeRename(RenameType.Normal),
                    strAName.TypeRename(RenameType.Normal));
//                if (rename != strChange) return strChange;
            }
            if (bFlag[(int)RenameType.ToUpper])
            {
                rename = rename.Replace(
                    strBName.TypeRename(RenameType.ToUpper),
                    strAName.TypeRename(RenameType.ToUpper));
                //                if (rename != strChange) return strChange;
            }
            if (bFlag[(int)RenameType.ToLower])
            {
                rename = rename.Replace(
                    strBName.TypeRename(RenameType.ToLower),
                    strAName.TypeRename(RenameType.ToLower));
                //                if (rename != strChange) return strChange;
            }
            if (bFlag[(int)RenameType.FirstToUpper])
            {
                rename = rename.Replace(
                strBName.TypeRename(RenameType.FirstToUpper),
                strAName.TypeRename(RenameType.FirstToUpper));
                //                if (rename != strChange) return strChange;
            }
            if (bFlag[(int)RenameType.FirstToLower])
            {
                rename = rename.Replace(
                strBName.TypeRename(RenameType.FirstToLower),
                strAName.TypeRename(RenameType.FirstToLower));
                //                if (rename != strChange) return strChange;
            }
            if (bFlag[(int)RenameType.ToTitleCase])
            {
                rename = rename.Replace(
                strBName.TypeRename(RenameType.ToTitleCase),
                strAName.TypeRename(RenameType.ToTitleCase));
            }
            return rename;
        }
        //-------------------------------------------------------------------
        // エンコード
        //-------------------------------------------------------------------
        static public Encoding GetEncoding(string strFile)
        {
            string strEncode;
            if (Path.GetExtension(strFile).ToUpper() == ".C" ||
                Path.GetExtension(strFile).ToUpper() == ".H" ||
                Path.GetExtension(strFile).ToUpper() == ".CPP")
            {
                strEncode = "sjis";
            }
            else
            {
                strEncode = "utf-8";
            }
            return System.Text.Encoding.GetEncoding(strEncode);
        }
        //-------------------------------------------------------------------
        // リネーム
        //-------------------------------------------------------------------
        static public string SetRename(bool[] bFlag, string strBString, string strBName, string strAName, string strPlus)
        {
            if (strBString == "" || strBName == "" || strAName == "") return "";
            string strAString;
            RenameString renameString = new RenameString(strBString);
            RenameString renameB = new RenameString(strBName);
            RenameString renameA = new RenameString(strAName);

            strAString = renameString.ReplaceRename(bFlag, renameB, renameA);
            if (/*strBString == strAString && */bFlag[(int)RenameType.PreUP] == true)
            {
                renameB = renameB.PlusRename(strPlus);
                renameA = renameA.PlusRename(strPlus);
                strAString = renameString.ReplaceRename(bFlag, renameB, renameA);
            }
            return strAString;
        }
        //-------------------------------------------------------------------
        // リネーム
        //-------------------------------------------------------------------
        static public void RichTextBoxChangeColor(bool[] bFlag, RichTextBox strBefreText, string strBString)
        {
            //リッチテキストボックスは文字列に代入しないと重い
            string strText = strBefreText.Text;
            if (strBString == "" || strText == "") return;
            //クリアも決して軽くはない
            strBefreText.SelectAll();
            strBefreText.SelectionBackColor = strBefreText.BackColor;
            RenameString renameB = new RenameString(strBString);
            for (int i = 0; i < strText.Length; i++)
            {
                for (int j = 0; j < (int)RenameType.MaxRename - 1; j++)
                {
                    if (bFlag[(int)(RenameType)j] == false) break;
                    string strWord = renameB.TypeRename((RenameType)j);
                    if (i + strWord.Length >= strText.Length) break;
                    if (strText.Substring(i, strWord.Length) == strWord)
                    {
                        strBefreText.Select(i, strWord.Length);
                        strBefreText.SelectionBackColor = Color.Green;
                        i += strWord.Length;
                        break;
                    }
                }
            }
        }
        //-------------------------------------------------------------------
        // パスの最後に\を追加、但しルートはしない
        //-------------------------------------------------------------------
        static public string PathLastDel(string strString)
        {
            if (strString[strString.Length - 1] == '\\' && strString[strString.Length - 2] != ':')
            {
                strString = strString.Remove(strString.Length - 1);
            }
            return strString;
        }
    }
}
